using System;
using NodeCanvas.Variables;

namespace NodeCanvas.Actions
{
	[Category("✫ Utility")]
	[Description("Alternative use to check a blackboard boolean, float or int variable")]
	public class CheckExpression : ConditionTask
	{
		public string expression;

		private string leftVar;

		private string operation;

		private string rightVar;

		private Type type;

		private object leftValue;

		private object rightValue;

		private string error;

		protected override string info
		{
			get
			{
				return (!string.IsNullOrEmpty(error)) ? error : ("'" + expression + "'");
			}
		}

		protected override bool OnCheck()
		{
			string[] array = expression.Split(' ');
			if (array.Length != 3 || string.IsNullOrEmpty(array[2]))
			{
				return Error("Wrong format");
			}
			leftVar = array[0];
			operation = array[1];
			rightVar = array[2];
			leftValue = base.blackboard.GetDataValue<object>(leftVar);
			if (leftValue == null)
			{
				return Error("No variable exists");
			}
			type = leftValue.GetType();
			rightValue = null;
			VariableData data = base.blackboard.GetData(rightVar, type);
			if (data != null)
			{
				rightValue = data.objectValue;
			}
			error = null;
			try
			{
				if (type == typeof(bool))
				{
					return BoolCheck();
				}
				if (type == typeof(float))
				{
					return FloatCheck();
				}
				if (type == typeof(int))
				{
					return IntCheck();
				}
			}
			catch
			{
				return Error("Parsing Error");
			}
			return Error("Unsupported Variable Type");
		}

		private bool Error(string err)
		{
			error = "<color=#d63e3e>" + err + "</color>";
			return false;
		}

		private bool BoolCheck()
		{
			if (rightValue == null)
			{
				rightValue = bool.Parse(rightVar);
			}
			if (operation == "==")
			{
				return (bool)leftValue == (bool)rightValue;
			}
			if (operation == "!=")
			{
				return (bool)leftValue != (bool)rightValue;
			}
			return Error("Wrong Format");
		}

		private bool FloatCheck()
		{
			if (rightValue == null)
			{
				rightValue = float.Parse(rightVar);
			}
			if (operation == "==")
			{
				return (float)leftValue == (float)rightValue;
			}
			if (operation == "!=")
			{
				return (float)leftValue != (float)rightValue;
			}
			if (operation == ">")
			{
				return (float)leftValue > (float)rightValue;
			}
			if (operation == "<")
			{
				return (float)leftValue < (float)rightValue;
			}
			if (operation == ">=")
			{
				return (float)leftValue >= (float)rightValue;
			}
			if (operation == "<=")
			{
				return (float)leftValue <= (float)rightValue;
			}
			return Error("Wrong Format");
		}

		private bool IntCheck()
		{
			if (rightValue == null)
			{
				rightValue = int.Parse(rightVar);
			}
			if (operation == "==")
			{
				return (int)leftValue == (int)rightValue;
			}
			if (operation == "!=")
			{
				return (int)leftValue != (int)rightValue;
			}
			if (operation == ">")
			{
				return (int)leftValue > (int)rightValue;
			}
			if (operation == "<")
			{
				return (int)leftValue < (int)rightValue;
			}
			if (operation == ">=")
			{
				return (int)leftValue >= (int)rightValue;
			}
			if (operation == "<=")
			{
				return (int)leftValue <= (int)rightValue;
			}
			return Error("Wrong Format");
		}
	}
}
